package com.example.onion.hxchat.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.onion.hxchat.room.entity.Info;
import com.example.onion.hxchat.room.entity.User;
import com.example.superyi.myteam.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author:onion
 * @data: 2020/7/26
 */
public class SearchFriendDisplayAdapter extends RecyclerView.Adapter<SearchFriendDisplayAdapter.ViewHolder>{
    private List<User> mList = new ArrayList<>();
    private SearchFriendDisplayAdapter.OnItemClickListener mOnItemClickListener;
    private SearchFriendDisplayAdapter.DataGetterListener mDataGetterListener;
    private Context mContext;

    public SearchFriendDisplayAdapter(Context context, List<User> mList) {
        this.mContext = context;
        this.mList = mList;
    }

    public SearchFriendDisplayAdapter()
    {

    }

    public void setmList(List<User> mList) {
        this.mList = mList;
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        ImageView searchFriendImg;      //查询添加好友页面子项的用户头像
        TextView searchFriendName;  //查询添加好友页面显示的用户名称
        TextView searchFriendSpy; //查询添加好友的用户专业
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            searchFriendImg = itemView.findViewById(R.id.search_friend_img_wc);
            searchFriendName = itemView.findViewById(R.id.search_friend_name_wc);
            searchFriendSpy = itemView.findViewById(R.id.search_friend_spy_wc);
        }
    }

    @NonNull
    @Override
    public SearchFriendDisplayAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_search_friend, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SearchFriendDisplayAdapter.ViewHolder holder, final int position) {
        User userBaen = mList.get(position);
        holder.searchFriendImg.setImageResource(R.drawable.user);
        holder.searchFriendName.setText(userBaen.getUserName());
        holder.searchFriendSpy.setText(userBaen.getUserSpy());

        //适配器只负责得到数据吧，跳转页面由从适配器获取到数据的Activity或者Fragment去选择跳转到哪个页面
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onClick(position);
                    //通过recyclerview的position指定当前行的数据，回调给调用方
                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    mOnItemClickListener.onLongClick(position);
                    return false;
                }
            });
        }
        if(mDataGetterListener != null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    User userBaen = mList.get(position);
                    mDataGetterListener.getItemData(userBaen);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }


    public interface OnItemClickListener {
        void onClick(int position);

        void onLongClick(int position);

    }
    public void setOnItemClickListener(SearchFriendDisplayAdapter.OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }



    public interface DataGetterListener {//适配器内创建一个回调接口，获取每一行recyclerview的相关数据

        void getItemData(User userBaen);//将本页面的获得的infoBean数据通过接口回调给调用方
    }

    public void setDataGetterListener(SearchFriendDisplayAdapter.DataGetterListener dataGetterListener) {
        this.mDataGetterListener = dataGetterListener;
    }
}
